//
//  WKFriendHomePageVC.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/20.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKFriendHomePageVC.h"
#import "WKFriendInforHeadView.h"
#import "JXCategoryView.h"
#import "WKFansListVC.h"

@interface WKFriendHomePageVC ()<JXCategoryViewDelegate>
{
    SSNavigationBar *_naviBar;
    NSArray *_titleArr;
    WKFriendBaseView *_allView;
    WKFriendBaseView *_bookView;
    WKFriendBaseView *_beautysayView;
    WKFriendBaseView *_videoView;
    WKFriendBaseView *_questionView;
    
    WKFriendInforHeadView *_userHeaderView;
}
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@end

@implementation WKFriendHomePageVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
}

- (void)initViews{
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    _titleArr = @[@"全部",@"日记",@"美说",@"视频",@"问答"];
    _allView = [[WKFriendBaseView alloc]init];
    _bookView = [[WKFriendBaseView alloc]init];
    _beautysayView = [[WKFriendBaseView alloc]init];
    _videoView = [[WKFriendBaseView alloc]init];
    _questionView = [[WKFriendBaseView alloc]init];
    

    _listViewArray = @[_allView,_bookView,_beautysayView,_videoView,_questionView];
    
    _userHeaderView = [[WKFriendInforHeadView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, NavBarHeight+130)];
    
    _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44+NavBarHeight)];
    self.categoryView.titles = _titleArr;
    self.categoryView.backgroundColor = [UIColor whiteColor];
    self.categoryView.delegate = self;
    self.categoryView.titleSelectedColor = BACK_COLOR;
    self.categoryView.titleColor = UIColorFromHex(0x3C3C3C);
    self.categoryView.titleFont = PFSC_MediumFont(15);
    self.categoryView.titleColorGradientEnabled = NO;
    self.categoryView.titleLabelZoomEnabled = NO;

    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorLineViewColor = BACK_COLOR;
    lineView.indicatorLineWidth = 35;
    self.categoryView.indicators = @[lineView];
    
    _pagerView = [self preferredPagingView];
    [self.view addSubview:self.pagerView];
 
    self.categoryView.contentScrollView = self.pagerView.listContainerView.collectionView;
    
    //扣边返回处理，下面的代码要加上
    [self.pagerView.listContainerView.collectionView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
    [self.pagerView.mainTableView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
    self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0);
    
    //让mainTableView可以显示范围外
    self.pagerView.mainTableView.clipsToBounds = false;
    //让头图的布局往上移动naviHeight高度，填充导航栏下面的内容
    _userHeaderView.frame = CGRectMake(0, -NavBarHeight, ScreenWidth,  130+NavBarHeight);
    
    UIImageView *bgimgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    bgimgview.image = ImageNamed(@"tableviewbgimg.jpg");
    bgimgview.contentMode = UIViewContentModeScaleAspectFill;
    bgimgview.layer.masksToBounds = YES;
    self.pagerView.mainTableView.backgroundView = bgimgview;
    
    @weakify(self);
    _userHeaderView.block = ^(NSInteger index) {
        @strongify(self);
        [self headerViewClick:index];
    };

    
    [self initMynavigationBar];
}



- (JXPagerView *)preferredPagingView {
    return [[JXPagerView alloc] initWithDelegate:self];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.pagerView.frame = self.view.bounds;
}

#pragma mark - JXPagerViewDelegate

- (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView {
    return _userHeaderView;
}

- (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView {
    return 130;
}

- (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
    return 44;
}

- (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
    return self.categoryView;
}

- (NSArray<id<JXPagerViewListViewDelegate>> *)listViewsInPagerView:(JXPagerView *)pagerView {
    return self.listViewArray;
}

#pragma mark - JXCategoryViewDelegate

- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

- (void)initMynavigationBar{
    _naviBar = [[SSNavigationBar alloc]initUsernaviBarWithFrame:CGRectMake(0, 0, ScreenWidth, NavBarHeight)];
    _naviBar.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_naviBar];
    [_naviBar displayUserInformationWithName:@"一直爱睡觉的猫" headerimgUrl:@"http://b-ssl.duitang.com/uploads/item/201410/04/20141004162939_VERQc.jpeg"];
    [_naviBar configMynavibarStyle:SSNavigationBarWhiteStyle];
    [_naviBar hiddenNaviBarLine];
    [_naviBar showShareBarItem];
    [_naviBar hiddenUserinformation];
    @weakify(self);
    _naviBar.backBlock = ^{
        @strongify(self);
        [self backController];
    };
}

- (void)mainTableViewDidScroll:(UIScrollView *)scrollView {
    
    NSLog(@".....%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y >=130) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        _naviBar.backgroundColor = [UIColor whiteColor];
        [_naviBar showNaviBarLine];
        [_naviBar showUserinformation];
        [_naviBar configMynavibarStyle:SSNavigationBarBlackStyle];
    
    }else{
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        _naviBar.backgroundColor = [UIColor clearColor];
        [_naviBar hiddenNaviBarLine];
        [_naviBar hiddenUserinformation];
        [_naviBar configMynavibarStyle:SSNavigationBarWhiteStyle];
        
    }
}


#pragma mark 关注 粉丝 点赞
- (void)headerViewClick:(NSInteger)index{
    if (index < 2 ) {
        WKFansListVC *fanslistVC = [[WKFansListVC alloc]init];
        fanslistVC.isFans = index == 1 ? YES : NO ;
        [self.navigationController pushViewController:fanslistVC animated:YES];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
